package com.sharepics.ws;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharepics.bean.Album;
import com.sharepics.bean.Picture;
import com.sharepics.bean.XMLAlbum;
import com.sharepics.bean.XMLPicture;
import com.sharepics.dao.AlbumDAO;
import com.sharepics.dao.PictureDAO;
import com.sharepics.services.AlbumService;
import com.sharepics.utils.bean.BeanUtils;

@RequestMapping("/ws/picture")
@Controller
public class PictureWS {

	@Autowired
	PictureDAO pictureDAO;
	
	@Autowired
	AlbumService albumService;
	
    @RequestMapping(value="/{id}",method = RequestMethod.GET)
    @ResponseBody
    public XMLPicture get(@PathVariable Long id) {
    	Picture pic = pictureDAO.get(id);
		XMLPicture xmlPicture = BeanUtils.get(pic);
    	return xmlPicture;
    	
    }
    
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public XMLPicture add(@RequestBody XMLPicture xmlPic) {
		Picture pic = BeanUtils.get(xmlPic);
    	System.out.println(pic);
    	//pictureDAO.save(pic);
    	Picture picSaved = null;
    	try {
    		picSaved = albumService.addPicture(pic, xmlPic.getAlbumId(),xmlPic.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		XMLPicture returnPic = BeanUtils.get(picSaved);
    	return returnPic;
    	
    }
    
    
}
